<!--
 * @Description:退款管理
-->
<template>
  <div class="registered-container refund appMainWarp">
    <el-form :inline="true" :model="searchKey" class="demo-form-inline">

      <el-form-item label="退款时间:">
        <el-date-picker
          v-model="searchKey.time"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
        />
      </el-form-item>
      <el-form-item label="支付方式:">
        <el-select v-model="searchKey.pay_style" placeholder="请选择">
          <el-option
            v-for="item in searchKey.PayStyle"
            :key="item.pay_no"
            :label="item.name"
            :value="item.pay_no"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="选择支付场景:">
        <el-select v-model="searchKey.system_no" multiple clearable placeholder="请选择支付场景">
          <el-option
            v-for="item in searchKey.System"
            :key="item.pay_scene_no"
            :label="item.pay_scene_name"
            :value="item.pay_scene_no"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型:">
        <el-cascader
          v-model="searchKey.bill_type"
          :options="searchKey.BillType"
          :props="props"
          change-on-select
          clearable
        />
      </el-form-item>
      <el-form-item label="退款状态:">
        <el-select v-model="searchKey.status" placeholder="请选择">
          <el-option label="全部" value="" />
          <el-option label="退款成功" value="1" />
          <el-option label="退款失败" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付场景订单号:">
        <el-input v-model="searchKey.outtradeno" placeholder="请输入支付场景订单号" />
      </el-form-item>
      <el-form-item label="退款金额:">
        <el-input v-model="searchKey.refundfee" placeholder="请输入退款金额" />
      </el-form-item>
      <el-form-item label="患者id:">
        <el-input v-model="searchKey.patientid" placeholder="请输入患者id" />
      </el-form-item>

      <el-form-item label="患者姓名:">
        <el-input v-model="searchKey.patientiname" placeholder="请输入患者姓名" />
      </el-form-item>

      <el-form-item label="交易流水号:">
        <el-input v-model="searchKey.order_no" placeholder="请输入交易流水号" />
      </el-form-item>
      <!-- <el-form-item label="操作员:">
        <el-input v-model="searchKey.operator" placeholder="请输入操作员" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="onLoad">查询</el-button>
      </el-form-item>
    </el-form>

    <template>
      <el-table
        ref="table"
        v-loading="page.tableLoading"
        v-el-height-adaptive-table="{bottomOffset: 90}"
        max-height="500px"
        center
        class="table"
        :data="searchDate"
        stripe
        border
      >
        <el-table-column type="index" width="90" label="序号" />
        <el-table-column label="患者ID" align="center">
          <template slot-scope="scope">{{ scope.row.patient_id||'-' }}</template>
        </el-table-column>
        <el-table-column label="患者姓名" align="center">
          <template slot-scope="scope">{{ scope.row.patient_name||'-' }}</template>
        </el-table-column>
        <el-table-column label="支付场景" align="center">
          <template slot-scope="scope">{{ scope.row.pay_scene||'-' }}</template>
        </el-table-column>
        <el-table-column label="支付场景订单号" align="center">
          <template slot-scope="scope">{{ scope.row.out_trade_no||'-' }}</template>
        </el-table-column>
        <el-table-column label="交易流水号" align="center">
          <template slot-scope="scope">{{ scope.row.trade_no ||'-' }}</template>
        </el-table-column>
        <el-table-column label="退款订单号" align="center">
          <template slot-scope="scope">{{ scope.row.refund_no ||'-' }}</template>
        </el-table-column>
        <el-table-column label="业务类型" align="center">
          <template slot-scope="scope">{{ scope.row.bill_type_name ||'-' }}</template>
        </el-table-column>
        <el-table-column label="交易金额" align="center">
          <template slot-scope="scope">{{ scope.row.pay_fee ||'-' }}</template>
        </el-table-column>
        <el-table-column label="退款金额" align="center">
          <template slot-scope="scope">{{ scope.row.refund_fee||'-' }}</template>
        </el-table-column>
        <el-table-column label="支付方式" align="center">
          <template slot-scope="scope">
            {{ scope.row.pay_style_name }}
          </template>
        </el-table-column>
        <el-table-column label="交易状态" align="center">
          <template slot-scope="scope">{{ scope.row.status ||'-' }}</template>
        </el-table-column>
        <el-table-column label="交易时间" align="center">
          <template slot-scope="scope">{{ scope.row.pay_time ||'-' }}</template>
        </el-table-column>
        <el-table-column label="退款时间" align="center">
          <template slot-scope="scope">{{ scope.row.refund_time||'-' }}</template>
        </el-table-column>
        <el-table-column label="操作员编号" align="center">
          <template slot-scope="scope">{{ scope.row.operator||'-' }}</template>
        </el-table-column>
        <el-table-column label="备注" align="center">
          <template slot-scope="scope">{{ scope.row.return_msg||'-' }}</template>
        </el-table-column>
      </el-table>
    </template>

    <pagination
      :total="page.totalRow"
      :page.sync="page.currentPage"
      :limit.sync="page.pageSize"
      @pagination="onSearch"
    />

  </div>
</template>

<script>
import elHeightAdaptiveTable from '@/directive/el-table'
import { getConfigType } from '@/api/transactRecords/index'
import { refundrecord } from '@/api/refundRecorded/index'
import Pagination from '@/components/Pagination' // 分页
export default {
  name: 'Record',
  components: {
    Pagination
  },
  directives: { elHeightAdaptiveTable },

  data() {
    return {
      props: {
        label: 'name',
        value: 'bill_no',
        children: 'child'
      },
      quaflat: 0, // 按钮默认
      date: 1, // 按天月年
      searchKey: {
        order_no: '', // 订单号
        time: '', // 账单日期
        pay_style: '', // 支付方式
        PayStyle: [],
        system_no: '', // 支付场景
        System: [],
        outtradeno: '', // 支付场景订单号
        refundfee: '',
        bill_type: [], // 业务类型
        BillType: [],
        patientid: '',
        status: '',
        operator: '',
        patientiname: ''
      },

      searchDate: [],

      addModel: {
        flag: false,
        item: {
          id: '',
          name: '', // 角色名称
          status: 1 // 1:启用 0:禁用
        }
      },

      page: {
        tableLoading: false,
        currentPage: 1, // 当前页
        pageSize: 20, // 默认条数
        totalRow: 0, // 总条数
        totalPage: 0 // 总页数
      },
      minDate: '',
      maxDate: '',
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.minDate = minDate
          this.maxDate = maxDate
        },
        disabledDate: (time) => { // 查询时间跨度为365天
          if (this.minDate) {
            var range = 365 * 24 * 3600 * 1000
            return time.getTime() > Date.now() || time.getTime() > (this.minDate.getTime() + range) || time.getTime() < (this.minDate.getTime() - range)
          }
          return time.getTime() > Date.now()
        }
      }
      // pickerOptions: { // 日期不能点击
      //   disabledDate(time) {
      //     return time.getTime() > Date.now() || time.getTime() === Date.now() || time.getTime() < new Date().setFullYear((new Date().getFullYear() - 1))
      //   }
      // }
    }
  },
  watch: {
    'searchKey.time': {
      deep: true,
      handler(val) {
        if (val == null) {
          this.minDate = null
        }
      }
    }
  },
  mounted() {
    this.getConfig()
    this.onLoad()
  },
  methods: {
    // 系统配置搜索
    getConfig() {
      var that = this
      var params = {}
      getConfigType(params).then(res => {
        that.searchKey.PayStyle = res.data.payment // 支付方式
        that.searchKey.System = res.data.scene// 场景
        that.searchKey.BillType = res.data.bill// 业务类型
      })
        .catch(() => {
        })
    },
    // 验证订单号
    suborder_no() {
      const id = this.searchKey.order_no
      const reg = /^[0-9a-zA-Z]+$/
      if (!reg.test(id)) {
        this.$message({
          type: 'warning',
          message: '请输入正确的订单号'
        })
        this.searchKey.order_no = ''
        return false
      }
    },
    // 查询列表
    onLoad() {
      this.page.currentPage = 1
      this.onSearch()
    },
    onSearch() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      if (that.searchKey.time && that.searchKey.time.length > 0) {
        params.starttime = that.searchKey.time[0]
        params.endtime = that.searchKey.time[1]
      } else {
        params.starttime = ''
        params.endtime = ''
      }
      params.paystyleno = that.searchKey.pay_style
      params.payscene = that.searchKey.system_no.toString()
      params.billtypeno = that.searchKey.bill_type.length > 1 ? that.searchKey.bill_type[1] : that.searchKey.bill_type[0] || ''
      params.outtradeno = that.searchKey.outtradeno
      params.refundfee = that.searchKey.refundfee
      params.patientid = that.searchKey.patientid
      params.patientiname = that.searchKey.patientiname
      params.tradeno = that.searchKey.order_no
      params.status = that.searchKey.status
      // params.operator = that.searchKey.operator
      that.page.tableLoading = true
      refundrecord(params)
        .then(res => {
          that.page.tableLoading = false
          that.searchDate = res.data.result
          that.page.totalPage = res.data.pageSum
          that.page.totalRow = parseInt(res.data.count)
        })
        .catch(() => {
          that.page.tableLoading = true
        })
    }

  }
}
</script>

<style lang="scss" scoped>
.refund {
  position: relative;
  width: 100%;
  .demo-form-inline {
    .el-button--primary {
      background-color: #579ef8 !important;
      color: #fff !important;
    }
   .el-button {
      background-color: #e8f4ff;
      color: #579ef8;
      border: 1px solid rgba(87, 158, 248, 1);
    }
  }
}
</style>
